﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using MvcMovie.Repository;

namespace MvcMovie.Services
{
    // 普通类的依赖注入，只支持构造函数,
    //使用该类的时候需要先将该类(TestService)注册到DI容器中:services.AddSingleton<TestService>();
    //调用如下语句即可使用:var service = serviceProvider.GetRequiredService<TestService>();
    public class TestService
    {
        private ITodoRepository _repository;
        public TestService(ITodoRepository r)
        {
            _repository = r;
        }

        public void Show()
        {
            Console.WriteLine(_repository.AllItems);
        }
    }


    public class TestService3
    {
        private IHttpContextAccessor _httpContextAccessor;
        public TestService3(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public void Show()
        {
            var httpContext = _httpContextAccessor.HttpContext;//获取上下文对象实例
            Console.WriteLine(httpContext.Request.Host.Value);
        }
    }
}
